package com.ruoyi.lzxt.user.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.lzxt.user.domain.entity.Employee;
import com.ruoyi.lzxt.user.service.EmployeeService;
import com.ruoyi.lzxt.user.service.LoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 力阵用户
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lzxt")
public class LoginController {
    private final LoginService loginService;

    private final EmployeeService employeeService;

    /**
     * 力阵用户登录
     * @param loginBody
     * @return
     */
    @SaIgnore
    @PostMapping("/login")
    public R<Map<String, Object>> login(@Validated @RequestBody LoginBody loginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
            loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * 获取力阵用户列表
     * @return
     */
    @GetMapping("/users")
    public R<Map<String, Object>> getUsers(){
        Map<String, Object> ajax = new HashMap<>();
        List<Employee> list = employeeService.list();
        ajax.put("list",list);
        return R.ok(ajax);
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @GetMapping("/userInfoOnToken")
    public R<Map<String, Object>> getUserInfo(){
        Map<String, Object> ajax = new HashMap<>();
//        List<Employee> list = employeeService.list();

        Employee employee = employeeService.selectUserByUserName("yankuilin");
        ajax.put("user",employee);
        return R.ok(ajax);
    }
}
